package com.manager.devicemanager.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manager.devicemanager.config.ActiveMQListener;
import com.manager.devicemanager.entity.TClientSystemFile;
import com.manager.devicemanager.mapper.TClientSystemFileMapper;
import com.manager.devicemanager.service.ITClientSystemFileService;
import com.manager.devicemanager.util.MqProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wpf
 * @since 2025-08-02
 */
@Service
public class TClientSystemFileServiceImpl extends ServiceImpl<TClientSystemFileMapper, TClientSystemFile> implements ITClientSystemFileService {
  @Autowired
  private MqProducer mqProducer;
  @Autowired
  private ActiveMQListener activeMQListener;
  @Autowired
  private TClientSystemFileMapper tClientSystemFileMapper;
    @Override
    public void upgrade(String id,String devicetopic) {
        TClientSystemFile tClientSystemFile = tClientSystemFileMapper.selectById(id);
        JSONObject message = new JSONObject();
        message.put("filepath", "/api/download/" + tClientSystemFile.getFilepath());
        message.put("type", "升级");
        String mqttMessage = message.toString();
        if ("ic".equals(tClientSystemFile.getDevicetype())) {
            if ("ic1".equals(devicetopic)) {
                mqProducer.sendStringTopic(activeMQListener.publishICTopic+"/update",mqttMessage);

            } else if ("ic2".equals(devicetopic)) {
                mqProducer.sendStringTopic(activeMQListener.publishIC2Topic+"/update",mqttMessage);
            }else {
                mqProducer.sendStringTopic(activeMQListener.publishICTopic+"/update",mqttMessage);
                mqProducer.sendStringTopic(activeMQListener.publishIC2Topic+"/update",mqttMessage) ;
            }
        } else if ("pd".equals(tClientSystemFile.getDevicetype())) {
            mqProducer.sendStringTopic(activeMQListener.publishPDTopic+"/update",mqttMessage);
        }
    }
}
